﻿Namespace News

    <Authorize>
    Public Class AdminController
        Inherits AzoxController

        '
        ' GET: /news/admin

        Function Index(Optional pageIndex As Integer = 0) As ActionResult
            Dim pageSize As Double = My.Settings.PageSize
            Dim totalRecords As Double = Db.News.Count
            ViewBag.PageIndex = pageIndex
            ViewBag.PageCount = CInt(Math.Ceiling(totalRecords / pageSize))

            ViewBag.Title = "Список новостей"
            ViewBag.CreateUrl = Url.Action("create")

            Return View("admin", Db.News.OrderByDescending(Function(m) m.PublishDate).Skip(pageIndex * CInt(pageSize)).Take(CInt(pageSize)).ToList())
        End Function

        Function Create() As ActionResult
            ViewBag.Title = "Создание новости"
            ViewBag.ImageUrl = GetImageUrl(Guid.Empty, ImageSize.Small)

            Return View(New NewsItem With {.PublishDate = DateTime.Now.Date})
        End Function

        <HttpPost>
        <ValidateAntiForgeryToken>
        Function Create(model As NewsItem, imageFile As HttpPostedFileBase) As ActionResult
            If ModelState.IsValid Then
                model.Id = Guid.NewGuid
                model.ThumbnailId = SaveImage(imageFile, My.Settings.LargeImageWidth, My.Settings.MediumImageWidth, My.Settings.SmallImageWidth)

                Db.News.Add(model)
                Db.SaveChanges()

                Return RedirectToLocal(String.Empty)
            End If

            ViewBag.Title = "Создание новости"
            ViewBag.ImageUrl = GetImageUrl(Guid.Empty, ImageSize.Small)

            Return View(model)
        End Function

        Function Edit(Optional id As Guid = Nothing) As ActionResult
            Dim model As NewsItem = Db.News.Find(id)
            If IsNothing(model) Then
                Return HttpNotFound()
            End If

            ViewBag.Title = "Редактирование новости"
            ViewBag.ImageUrl = GetImageUrl(model.ThumbnailId, ImageSize.Small)

            Return View(model)
        End Function

        <HttpPost>
        <ValidateAntiForgeryToken>
        Function Edit(model As NewsItem, imageFile As HttpPostedFileBase, returnUrl As String) As ActionResult
            If ModelState.IsValid Then
                model.ThumbnailId = UpdateImage(imageFile, My.Settings.LargeImageWidth, My.Settings.MediumImageWidth, My.Settings.SmallImageWidth, model.ThumbnailId)
                Db.Entry(model).State = EntityState.Modified
                Db.SaveChanges()

                Return RedirectToLocal(returnUrl)
            End If

            ViewBag.Title = "Редактирование новости"
            ViewBag.ImageUrl = GetImageUrl(model.ThumbnailId, ImageSize.Small)

            Return View(model)
        End Function

        Function Delete(Optional id As Guid = Nothing) As ActionResult
            Dim model As NewsItem = Db.News.Find(id)
            If IsNothing(model) Then
                Return HttpNotFound()
            End If

            ViewBag.Title = "Удаление новости"

            Return View(model)
        End Function

        <HttpPost>
        <ActionName("Delete")>
        <ValidateAntiForgeryToken()>
        Function DeleteConfirm(id As Guid) As ActionResult
            Dim model As NewsItem = Db.News.Find(id)
            DeleteImage(model.ThumbnailId)
            Db.News.Remove(model)
            Db.SaveChanges()

            Return RedirectToLocal(String.Empty)
        End Function
    End Class

End Namespace
